//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.stock;

import java.awt.event.*;
import java.beans.*;
import javax.swing.*;
import javax.swing.event.*;
import org.jpgolay.gui.fixed.BasicMenu;
import org.jpgolay.gui.fixed.TableViewer;
import org.jpgolay.gui.iconBar.*;
import org.jpgolay.me.agency.Agency;
import org.jpgolay.me.batch.*;
import org.jpgolay.me.customer.CustomerPanelController;
import org.jpgolay.me.delivery.DeliveryController;
import org.jpgolay.me.item.ItemPanelController;
import org.jpgolay.me.order.NewOrderController;
import org.jpgolay.me.order.OrderStockController;
import org.jpgolay.me.persistence.ObjectCRUD;
import org.jpgolay.me.piece.PiecesPrint;
import org.jpgolay.me.piece.list.PieceListController;
import org.jpgolay.me.preparation.PreparationController;
import org.jpgolay.me.salesperson.SalesPerson;
import org.jpgolay.me.sending.*;
import org.jpgolay.me.statistic.StatisticsWPController;
import org.jpgolay.me.stock.inventory.InventoryListController;
import org.jpgolay.me.stock.log.LogStockListController;
import org.jpgolay.me.supplier.SupplierPanelController;
import org.jpgolay.me.util.BrowserControl;
import org.jpgolay.me.util.Parameter;

// Referenced classes of package org.jpgolay.me.stock:
//            StockGUI
public class StockController {

    private class MouseClickPieceList extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                actionOpenOrder(pieceListCtrl.getPieceType(), (String) sGui.getPiecesTableViewer().getTable().getValueAt(sGui.getPiecesTableViewer().getTable().getSelectedRow(), 0));
            }
        }

        MouseClickPieceList() {
        }
    }

    private class MouseClickDeliveryList extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                actionOpenOrder(2, (String) sGui.getDeliveryTableViewer().getTable().getValueAt(sGui.getDeliveryTableViewer().getTable().getSelectedRow(), 0));
            }
        }

        MouseClickDeliveryList() {
        }
    }

    private class MouseClickCustomerList extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                OrderStockController oCtrl = (OrderStockController) sGui.getSelectedPiece();
                if (oCtrl != null) {
                    oCtrl.setCustomer((String) sGui.getCustomerTableViewer().getTable().getValueAt(sGui.getCustomerTableViewer().getTable().getSelectedRow(), 0));
                }
            }
        }

        MouseClickCustomerList() {
        }
    }

    private class MouseClickSupplierList extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                OrderStockController oCtrl = (OrderStockController) sGui.getSelectedPiece();
                if (oCtrl != null) {
                    oCtrl.setSupplier((String) sGui.getSupplierTableViewer().getTable().getValueAt(sGui.getSupplierTableViewer().getTable().getSelectedRow(), 0));
                }
            }
        }

        MouseClickSupplierList() {
        }
    }

    private class MouseClickItemList extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                OrderStockController oCtrl = (OrderStockController) sGui.getSelectedPiece();
                if (oCtrl != null) {
                    oCtrl.setItem((String) sGui.getItemTableViewer().getTable().getValueAt(sGui.getItemTableViewer().getTable().getSelectedRow(), 0));
                }
            }
        }

        MouseClickItemList() {
        }
    }

    private class MouseClickPreparationList extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                OrderStockController oCtrl = (OrderStockController) sGui.getSelectedPiece();
                if (oCtrl != null && preparationCtrl != null) {
                    if (oCtrl.getPieceType() <= 2) {
                        preparationCtrl.setBatchChoice(oCtrl.getSelectedOrderRow(), (String) sGui.getPreparationTableViewer().getTable().getValueAt(sGui.getPreparationTableViewer().getTable().getSelectedRow(), 0));
                    } else {
                        JOptionPane.showMessageDialog(null, "La pi\350ce " + oCtrl.getPieceId() + " est incompatible avec le volet de pr\351paration !");
                    }
                }
            }
        }

        MouseClickPreparationList() {
        }
    }

    private class MouseClickBatchList extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                initReception();
                sGui.openReceptionPanel();
                batchCtrl.modifyBatch((String) sGui.getBatchList().getTableViewerPanel().getTable().getValueAt(sGui.getBatchList().getTableViewerPanel().getTable().getSelectedRow(), 0));
            }
        }

        MouseClickBatchList() {
        }
    }

    private class MouseClickSendingList extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                initSending();
                sGui.openSendingPanel();
                sendingCtrl.modifySending((String) sGui.getSendingList().getTableViewerPanel().getTable().getValueAt(sGui.getSendingList().getTableViewerPanel().getTable().getSelectedRow(), 0));
            }
        }

        MouseClickSendingList() {
        }
    }

    public StockController(Parameter param) {
        msgTab = "";
        key = 0;
        write = false;
        desktop = param.getDesktop();
        this.param = param;
        crud = new ObjectCRUD();
        initLaw();
        initAll();
    }

    private void initLaw() {
        if (param.getAccessLaw().length() >= 27 && param.getAccessLaw().charAt(27) == '1') {
            write = true;
        }
    }

    private boolean initAll() {
        if (sGui == null || sGui.isClosed()) {
            sGui = new StockGUI();
            sGui.setDefaultCloseOperation(0);
            sGui.setIconifiable(true);
            sGui.setResizable(false);
            sGui.setMaximizable(false);
            sGui.setClosable(true);
            sGui.setVisible(true);
            sGui.toFront();
            sGui.addInternalFrameListener(new InternalFrameAdapter() {
                public void internalFrameClosing(InternalFrameEvent evt) {
                    actionExit();
                }
            });
            sGui.getWelcomeCustomerButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initCustomer();
                }
            });
            sGui.getWelcomeDeliveryButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initDelivery();
                }
            });
            sGui.getWelcomeGroupButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initReception();
                }
            });
            sGui.getWelcomeItemButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initItem();
                }
            });
            sGui.getWelcomePaymentButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPreparation();
                }
            });
            sGui.getWelcomePiecesButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPiecesList();
                }
            });
            sGui.getWelcomeSupplierButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initSupplier();
                }
            });
            sGui.getWelcomeSendingButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initSending();
                }
            });
            sGui.getWelcomeInventoryButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initInventory();
                }
            });
            sGui.getPiecesCustomerButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initCustomer();
                }
            });
            sGui.getPiecesDeliveryButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initDelivery();
                }
            });
            sGui.getPiecesGroupButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initReception();
                }
            });
            sGui.getPiecesItemButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initItem();
                }
            });
            sGui.getPiecesPaymentButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPreparation();
                }
            });
            sGui.getPiecesPiecesButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPiecesList();
                }
            });
            sGui.getPiecesSupplierButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initSupplier();
                }
            });
            sGui.getPiecesSendingButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initSending();
                }
            });
            sGui.getPiecesInventoryButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initInventory();
                }
            });
            sGui.getDeliveryCustomerButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initCustomer();
                }
            });
            sGui.getDeliveryDeliveryButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initDelivery();
                }
            });
            sGui.getDeliveryGroupButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initReception();
                }
            });
            sGui.getDeliveryItemButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initItem();
                }
            });
            sGui.getDeliveryPaymentButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPreparation();
                }
            });
            sGui.getDeliveryPiecesButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPiecesList();
                }
            });
            sGui.getDeliverySupplierButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initSupplier();
                }
            });
            sGui.getDeliverySendingButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initSending();
                }
            });
            sGui.getDeliveryInventoryButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initInventory();
                }
            });
            sGui.getReceptionCustomerButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initCustomer();
                }
            });
            sGui.getReceptionDeliveryButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initDelivery();
                }
            });
            sGui.getReceptionReceptionButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initReception();
                }
            });
            sGui.getReceptionItemButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initItem();
                }
            });
            sGui.getReceptionPreparationButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPreparation();
                }
            });
            sGui.getReceptionPiecesButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPiecesList();
                }
            });
            sGui.getReceptionSupplierButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initSupplier();
                }
            });
            sGui.getReceptionSendingButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initSending();
                }
            });
            sGui.getReceptionInventoryButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initInventory();
                }
            });
            sGui.getPreparationCustomerButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initCustomer();
                }
            });
            sGui.getPreparationDeliveryButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initDelivery();
                }
            });
            sGui.getPreparationGroupButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initReception();
                }
            });
            sGui.getPreparationItemButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initItem();
                }
            });
            sGui.getPreparationPreparationButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPreparation();
                }
            });
            sGui.getPreparationPieceButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPiecesList();
                }
            });
            sGui.getPreparationSupplierButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initSupplier();
                }
            });
            sGui.getPreparationSendingButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initSending();
                }
            });
            sGui.getPreparationInventoryButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initInventory();
                }
            });
            sGui.getCustomerCustomerButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initCustomer();
                }
            });
            sGui.getCustomerDeliveryButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initDelivery();
                }
            });
            sGui.getCustomerGroupButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initReception();
                }
            });
            sGui.getCustomerItemButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initItem();
                }
            });
            sGui.getCustomerPaymentButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPreparation();
                }
            });
            sGui.getCustomerPiecesButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPiecesList();
                }
            });
            sGui.getCustomerSupplierButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initSupplier();
                }
            });
            sGui.getCustomerSendingButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initSending();
                }
            });
            sGui.getCustomerInventoryButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initInventory();
                }
            });
            sGui.getSupplierCustomerButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initCustomer();
                }
            });
            sGui.getSupplierDeliveryButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initDelivery();
                }
            });
            sGui.getSupplierGroupButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initReception();
                }
            });
            sGui.getSupplierItemButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initItem();
                }
            });
            sGui.getSupplierPaymentButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPreparation();
                }
            });
            sGui.getSupplierPiecesButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPiecesList();
                }
            });
            sGui.getSupplierSupplierButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initSupplier();
                }
            });
            sGui.getSupplierSendingButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initSending();
                }
            });
            sGui.getSupplierInventoryButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initInventory();
                }
            });
            sGui.getItemCustomerButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initCustomer();
                }
            });
            sGui.getItemDeliveryButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initDelivery();
                }
            });
            sGui.getItemReceptionButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initReception();
                }
            });
            sGui.getItemItemButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initItem();
                }
            });
            sGui.getItemPreparationButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPreparation();
                }
            });
            sGui.getItemPiecesButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPiecesList();
                }
            });
            sGui.getItemSupplierButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initSupplier();
                }
            });
            sGui.getItemSendingButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initSending();
                }
            });
            sGui.getItemInventoryButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initInventory();
                }
            });
            sGui.getSendingCustomerButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initCustomer();
                }
            });
            sGui.getSendingDeliveryButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initDelivery();
                }
            });
            sGui.getSendingReceptionButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initReception();
                }
            });
            sGui.getSendingItemButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initItem();
                }
            });
            sGui.getSendingPreparationButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPreparation();
                }
            });
            sGui.getSendingPiecesButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPiecesList();
                }
            });
            sGui.getSendingSupplierButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initSupplier();
                }
            });
            sGui.getSendingSendingButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initSending();
                }
            });
            sGui.getSendingInventoryButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initInventory();
                }
            });
            sGui.getInventoryCustomerButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initCustomer();
                }
            });
            sGui.getInventoryDeliveryButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initDelivery();
                }
            });
            sGui.getInventoryReceptionButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initReception();
                }
            });
            sGui.getInventoryItemButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initItem();
                }
            });
            sGui.getInventoryPreparationButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPreparation();
                }
            });
            sGui.getInventoryPiecesButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPiecesList();
                }
            });
            sGui.getInventorySupplierButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initSupplier();
                }
            });
            sGui.getInventorySendingButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initSending();
                }
            });
            sGui.getInventoryInventoryButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initInventory();
                }
            });
            sGui.getPiecesTabbedPane().addChangeListener(new ChangeListener() {
                public void stateChanged(ChangeEvent evt) {
                    actionRefreshMenuBarAndToolBar();
                }
            });
            sGui.getContentLeft().addChangeListener(new ChangeListener() {
                public void stateChanged(ChangeEvent evt) {
                    switch (sGui.getContentLeft().getSelectedIndex()) {
                        case 1: // '\001'
                            initBatchList();
                            break;

                        case 2: // '\002'
                            initSendingList();
                            break;

                        case 3: // '\003'
                            initLogStockList();
                            break;
                    }
                }
            });
            initMenu();
            initIconBar();
            initPieces();
            initWelcome();
            initIcon();
            return true;
        }
        if (sGui.isIcon()) {
            try {
                sGui.setIcon(false);
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
        }
        sGui.toFront();
        return false;
    }

    private void initMenu() {
        sGui.getMenu().getExit().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionExit();
            }
        });
        sGui.getMenu().getDelete().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionDelete();
            }
        });
        sGui.getMenu().getHelp().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionHelp();
            }
        });
        sGui.getMenu().getModify().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionModify();
            }
        });
        sGui.getMenu().getNew().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionNew();
            }
        });
        sGui.getMenu().getOpen().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionOpen();
            }
        });
        sGui.getMenu().getPrint().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionPrint(true);
            }
        });
        sGui.getMenu().getSave().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionSave();
            }
        });
        sGui.getMenu().getSave().setEnabled(false);
        sGui.getMenu().getModify().setEnabled(false);
        sGui.getMenu().getDelete().setEnabled(false);
        sGui.getMenu().getPrint().setEnabled(false);
        sGui.getMenu().getNew().setEnabled(false);
    }

    private void initIconBar() {
        sGui.getIconBar().addAnswerListener(new AnswerListener() {
            public void create(AnswerEvent e) {
                actionNew();
            }

            public void open(AnswerEvent e) {
                actionOpen();
            }

            public void modify(AnswerEvent e) {
                actionModify();
            }

            public void save(AnswerEvent e) {
                actionSave();
            }

            public void remove(AnswerEvent e) {
                actionDelete();
            }

            public void print(AnswerEvent e) {
                actionPrint(false);
            }

            public void help(AnswerEvent e) {
                actionHelp();
            }
        });
        sGui.getIconBar().setEnabledSave(false);
        sGui.getIconBar().setEnabledModify(false);
        sGui.getIconBar().setEnabledRemove(false);
        sGui.getIconBar().setEnabledPrint(false);
        sGui.getIconBar().setEnabledCreate(false);
    }

    private void initIcon() {
        quoteSales = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/quoteS16x16.png"));
        orderSales = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/orderS16x16.png"));
        deliverySales = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/deliveryS16x16.png"));
        billSales = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/billS16x16.png"));
        assetSales = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/assetS16x16.png"));
        orderBuys = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/orderB16x16.png"));
        billBuys = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/billB16x16.png"));
        quoteSalesM = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/mQuoteS16x16.png"));
        orderSalesM = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/mOrderS16x16.png"));
        deliverySalesM = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/mDeliveryS16x16.png"));
        billSalesM = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/mBillS16x16.png"));
        assetSalesM = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/mAssetS16x16.png"));
        orderBuysM = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/mOrderB16x16.png"));
        billBuysM = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/mBillB16x16.png"));
    }

    private void initPieces() {
    }

    private void initWelcome() {
        if (statWPCtrl == null) {
            statWPCtrl = new StatisticsWPController(sGui.getStatisticsWPGUI(), param, desktop);
        }
    }

    private void initPiecesList() {
        if (pieceListCtrl == null) {
            pieceListCtrl = new PieceListController(sGui.getPiecesSearchBox(), sGui.getPiecesTableViewer(), desktop);
            sGui.getPiecesTableViewer().getTable().addMouseListener(new MouseClickPieceList());
        }
    }

    private void initDelivery() {
        if (deliveryCtrl == null) {
            deliveryCtrl = new DeliveryController(sGui.getDeliverySearchBox(), sGui.getDeliveryTableViewer(), param);
            sGui.getDeliveryTableViewer().getTable().addMouseListener(new MouseClickDeliveryList());
        }
    }

    private void initReception() {
        if (batchCtrl == null) {
            batchCtrl = new BatchController(sGui.getBatch(), sGui.getBatchCmd(), param);
            batchCtrl.addPropertyChangeListener(new PropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent e) {
                    if (e.getNewValue().equals("checkCmd")) {
                        actionReceptionUpdatePiece();
                    } else if (e.getNewValue().equals("buyIdField")) {
                        actionReceptionPiece();
                    }
                }
            });
        }
    }

    private void initBatchList() {
        if (batchListCtrl == null) {
            batchListCtrl = new BatchListController(sGui.getBatchList(), param, desktop);
            sGui.getBatchList().getTableViewerPanel().getTable().addMouseListener(new MouseClickBatchList());
        }
    }

    private void initSendingList() {
        if (sendingListCtrl == null) {
            sendingListCtrl = new SendingListController(sGui.getSendingList(), param, desktop);
            sGui.getSendingList().getTableViewerPanel().getTable().addMouseListener(new MouseClickSendingList());
        }
    }

    private void initLogStockList() {
        if (logStockListCtrl == null) {
            logStockListCtrl = new LogStockListController(sGui.getLogStockList(), param, desktop);
        }
    }

    private void initPreparation() {
        if (preparationCtrl == null) {
            preparationCtrl = new PreparationController(sGui.getPreparationSearchBox(), sGui.getPreparationTableViewer(), sGui.getPreparationBatch(), param);
            sGui.getPreparationTableViewer().getTable().addMouseListener(new MouseClickPreparationList());
            preparationCtrl.addPropertyChangeListener(new PropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent e) {
                    actionPreparationUpdatePiece();
                }
            });
        }
    }

    private void initSending() {
        if (sendingCtrl == null) {
            sendingCtrl = new SendingController(sGui.getSending(), sGui.getSendingCmd(), param);
            sendingCtrl.addPropertyChangeListener(new PropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent e) {
                    if (e.getNewValue().equals("updateSalesPiece")) {
                        actionSendingUpdatePiece();
                    } else if (e.getNewValue().equals("orderIdField")) {
                        actionSendingPiece();
                    }
                }
            });
        }
    }

    private void initInventory() {
        if (inventoryListCtrl == null) {
            inventoryListCtrl = new InventoryListController(sGui.getInventoryList(), param);
        }
    }

    private void initCustomer() {
        if (customerCtrl == null) {
            customerCtrl = new CustomerPanelController(sGui.getCustomerSearchBox(), sGui.getCustomerTableViewer(), desktop);
            sGui.getCustomerTableViewer().getTable().addMouseListener(new MouseClickCustomerList());
        }
    }

    private void initSupplier() {
        if (supplierCtrl == null) {
            supplierCtrl = new SupplierPanelController(sGui.getSupplierSearchBox(), sGui.getSupplierTableViewer(), desktop);
            sGui.getSupplierTableViewer().getTable().addMouseListener(new MouseClickSupplierList());
        }
    }

    private void initItem() {
        if (itemCtrl == null) {
            itemCtrl = new ItemPanelController(sGui.getItemSearchBox(), sGui.getItemTableViewer(), desktop);
            sGui.getItemTableViewer().getTable().addMouseListener(new MouseClickItemList());
            itemCtrl.addPropertyChangeListener(new PropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent e) {
                    OrderStockController oCtrl = (OrderStockController) sGui.getSelectedPiece();
                    if (oCtrl != null) {
                        oCtrl.setItem((String) sGui.getItemTableViewer().getTable().getValueAt(0, 0));
                    }
                }
            });
        }
    }

    private void initNewOrder() {
        if (newOrderCtrl == null) {
            newOrderCtrl = new NewOrderController(desktop);
            newOrderCtrl.getOK().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    newOrderCtrl.actionOK();
                    actionNewOrder(newOrderCtrl.getTypePiece(), newOrderCtrl.getAgency(), newOrderCtrl.getSalesPerson());
                }
            });
            newOrderCtrl.getCANCEL().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    newOrderCtrl.actionCANCEL();
                }
            });
        } else {
            newOrderCtrl.init();
        }
    }

    private void actionNew() {
        initNewOrder();
    }

    private void actionOpen() {
        initPiecesList();
        sGui.openPiecesPanel();
    }

    private void actionSave() {
        OrderStockController oCtrl = (OrderStockController) sGui.getSelectedPiece();
        switch (oCtrl.getPieceType()) {
            case 0: // '\0'
                pieceIcon = quoteSales;
                break;

            case 1: // '\001'
                pieceIcon = orderSales;
                break;

            case 2: // '\002'
                pieceIcon = deliverySales;
                break;

            case 3: // '\003'
                pieceIcon = billSales;
                break;

            case 4: // '\004'
                pieceIcon = assetSales;
                break;

            case 5: // '\005'
                pieceIcon = orderBuys;
                break;

            case 6: // '\006'
                pieceIcon = billBuys;
                break;

            default:
                pieceIcon = quoteSales;
                break;
        }
        sGui.setIconPiece(pieceIcon);
        oCtrl.setEditable(false);
        sGui.getIconBar().setEnabledRemove(false);
        sGui.getIconBar().setEnabledSave(false);
        sGui.getIconBar().setEnabledModify(true);
        sGui.getMenu().getDelete().setEnabled(false);
        sGui.getMenu().getSave().setEnabled(false);
        sGui.getMenu().getModify().setEnabled(true);
    }

    private void actionPrint(boolean viewer) {
        OrderStockController oCtrl = (OrderStockController) sGui.getSelectedPiece();
        if (oCtrl.getPieceId() != null) {
            print = new PiecesPrint(oCtrl.getAgency(), oCtrl.getAddress(), oCtrl.getPiece(), oCtrl.getSalesPerson(), oCtrl.getRowsList(), viewer, param);
        }
    }

    private void actionExit() {
        customerCtrl = null;
        itemCtrl = null;
        supplierCtrl = null;
        pieceListCtrl = null;
        deliveryCtrl = null;
        statWPCtrl = null;
        batchCtrl = null;
        sendingListCtrl = null;
        batchListCtrl = null;
        logStockListCtrl = null;
        inventoryListCtrl = null;
        preparationCtrl = null;
        sGui.setVisible(false);
        sGui = null;
    }

    private void actionModify() {
        OrderStockController oCtrl = (OrderStockController) sGui.getSelectedPiece();
        switch (oCtrl.getPieceType()) {
            case 0: // '\0'
                pieceIcon = quoteSalesM;
                break;

            case 1: // '\001'
                pieceIcon = orderSalesM;
                break;

            case 2: // '\002'
                pieceIcon = deliverySalesM;
                break;

            case 3: // '\003'
                pieceIcon = billSalesM;
                break;

            case 4: // '\004'
                pieceIcon = assetSalesM;
                break;

            case 5: // '\005'
                pieceIcon = orderBuysM;
                break;

            case 6: // '\006'
                pieceIcon = billBuysM;
                break;

            default:
                pieceIcon = quoteSalesM;
                break;
        }
        sGui.setIconPiece(pieceIcon);
        oCtrl.setEditable(true);
        sGui.getIconBar().setEnabledRemove(true);
        sGui.getIconBar().setEnabledSave(true);
        sGui.getIconBar().setEnabledModify(false);
        sGui.getMenu().getDelete().setEnabled(true);
        sGui.getMenu().getSave().setEnabled(true);
        sGui.getMenu().getModify().setEnabled(false);
    }

    private void actionDelete() {
        OrderStockController oCtrl = (OrderStockController) sGui.getSelectedPiece();
        int answer = JOptionPane.showConfirmDialog(null, "Souhaitez vous vraiment supprimer la pi\350ce : " + oCtrl.getPieceId() + " ?", "Confirmation", 1, 2, null);
        switch (answer) {
            case -1:
            case 1: // '\001'
            case 2: // '\002'
            default:
                break;

            case 0: // '\0'
                if (actionRemovePiece(oCtrl)) {
                    sGui.removePiece();
                    sGui.getIconBar().setEnabledRemove(false);
                    sGui.getIconBar().setEnabledSave(false);
                    sGui.getIconBar().setEnabledModify(false);
                    sGui.getMenu().getDelete().setEnabled(false);
                    sGui.getMenu().getSave().setEnabled(false);
                    sGui.getMenu().getModify().setEnabled(false);
                } else {
                    JOptionPane.showMessageDialog(null, "Impossible de supprimer la piece " + oCtrl.getPieceId() + " !" + "\n(un autre utilisateur l'a peut-\352tre modifi\351e entre temps)");
                }
                break;
        }
    }

    private boolean actionRemovePiece(OrderStockController oCtrl) {
        switch (oCtrl.getPieceType()) {
            case 0: // '\0'
                return crud.delete(oCtrl.getPieceId(), oCtrl.getLastUpdatePiece(), 22);

            case 1: // '\001'
                return crud.delete(oCtrl.getPieceId(), oCtrl.getLastUpdatePiece(), 22);

            case 2: // '\002'
                return crud.delete(oCtrl.getPieceId(), oCtrl.getLastUpdatePiece(), 22);

            case 3: // '\003'
                return crud.delete(oCtrl.getPieceId(), oCtrl.getLastUpdatePiece(), 25);

            case 4: // '\004'
                return crud.delete(oCtrl.getPieceId(), oCtrl.getLastUpdatePiece(), 25);

            case 5: // '\005'
                return crud.delete(oCtrl.getPieceId(), oCtrl.getLastUpdatePiece(), 24);

            case 6: // '\006'
                return crud.delete(oCtrl.getPieceId(), oCtrl.getLastUpdatePiece(), 24);
        }
        return false;
    }

    private void actionHelp() {
        BrowserControl.displayURL("www.media-4-u.net");
    }

    private void actionRefreshMenuBarAndToolBar() {
        OrderStockController oCtrl = (OrderStockController) sGui.getSelectedPiece();
        if (oCtrl == null) {
            sGui.getIconBar().setEnabledRemove(false);
            sGui.getIconBar().setEnabledSave(false);
            sGui.getIconBar().setEnabledModify(false);
            sGui.getMenu().getDelete().setEnabled(false);
            sGui.getMenu().getSave().setEnabled(false);
            sGui.getMenu().getModify().setEnabled(false);
            sGui.getMenu().getPrint().setEnabled(false);
            sGui.getIconBar().setEnabledPrint(false);
        } else if (oCtrl.isEditable()) {
            sGui.getIconBar().setEnabledRemove(true);
            sGui.getIconBar().setEnabledSave(true);
            sGui.getIconBar().setEnabledModify(false);
            sGui.getMenu().getDelete().setEnabled(true);
            sGui.getMenu().getSave().setEnabled(true);
            sGui.getMenu().getModify().setEnabled(false);
        } else {
            sGui.getIconBar().setEnabledRemove(false);
            sGui.getIconBar().setEnabledSave(false);
            sGui.getMenu().getDelete().setEnabled(false);
            sGui.getMenu().getSave().setEnabled(false);
            if (oCtrl.isLocked()) {
                sGui.getIconBar().setEnabledModify(false);
                sGui.getMenu().getModify().setEnabled(false);
            } else {
                sGui.getIconBar().setEnabledModify(true);
                sGui.getMenu().getModify().setEnabled(true);
            }
        }
    }

    private void actionNewOrder(int pieceType, Agency agency, SalesPerson salesPerson) {
        OrderStockController oCtrl = new OrderStockController(getNewPiece(pieceType), agency, salesPerson, pieceType, param);
        sGui.addPiece(pieceId, pieceIcon, oCtrl, msgTab);
        sGui.getIconBar().setEnabledRemove(true);
        sGui.getIconBar().setEnabledSave(true);
        sGui.getIconBar().setEnabledModify(false);
        sGui.getMenu().getDelete().setEnabled(true);
        sGui.getMenu().getSave().setEnabled(true);
        sGui.getMenu().getModify().setEnabled(false);
        sGui.getMenu().getPrint().setEnabled(true);
        sGui.getIconBar().setEnabledPrint(true);
    }

    private Object getNewPiece(int type) {
        switch (type) {
            case 0: // '\0'
                msgTab = "DEVIS client";
                pieceIcon = quoteSalesM;
                pieceId = (String) crud.create(22, "D");
                return crud.read(pieceId, 22);

            case 1: // '\001'
                msgTab = "COMMANDE client";
                pieceIcon = orderSalesM;
                pieceId = (String) crud.create(22, "C");
                return crud.read(pieceId, 22);

            case 2: // '\002'
                msgTab = "BL client";
                pieceIcon = deliverySalesM;
                pieceId = (String) crud.create(22, "BL");
                return crud.read(pieceId, 22);

            case 3: // '\003'
                msgTab = "FACTURE client";
                pieceIcon = billSalesM;
                pieceId = (String) crud.create(25, "F");
                return crud.read(pieceId, 25);

            case 4: // '\004'
                msgTab = "AVOIR client";
                pieceIcon = assetSalesM;
                pieceId = (String) crud.create(25, "A");
                return crud.read(pieceId, 25);

            case 5: // '\005'
                msgTab = "COMMANDE fournisseur";
                pieceIcon = orderBuysM;
                pieceId = (String) crud.create(24, "C");
                return crud.read(pieceId, 24);

            case 6: // '\006'
                msgTab = "FACTURE fournisseur";
                pieceIcon = billBuysM;
                pieceId = (String) crud.create(24, "F");
                return crud.read(pieceId, 24);
        }
        return null;
    }

    private void actionOpenOrder(int pieceType, String pieceId) {
        if (pieceType == 1 || pieceType == 2 || pieceType == 5) {
            OrderStockController oCtrl = null;
            boolean newPiece = true;
            int countPiece = sGui.getPiecesCount();
            if (countPiece != 0) {
                for (int i = 0; i < countPiece; i++) {
                    if (sGui.getTitlePiece(i).equals(pieceId)) {
                        getOpenPiece(pieceType, pieceId);
                        sGui.setSelectedPiece(i, pieceIcon);
                        oCtrl = (OrderStockController) sGui.getSelectedPiece();
                        newPiece = false;
                    }
                }

            }
            if (newPiece) {
                oCtrl = new OrderStockController(getOpenPiece(pieceType, pieceId), pieceType, param);
                sGui.addPiece(pieceId, pieceIcon, oCtrl, msgTab);
            }
            sGui.getIconBar().setEnabledRemove(false);
            sGui.getIconBar().setEnabledSave(false);
            sGui.getMenu().getDelete().setEnabled(false);
            sGui.getMenu().getSave().setEnabled(false);
            sGui.getMenu().getPrint().setEnabled(true);
            sGui.getIconBar().setEnabledPrint(true);
            if (oCtrl.isLocked()) {
                sGui.getIconBar().setEnabledModify(false);
                sGui.getMenu().getModify().setEnabled(false);
            } else {
                sGui.getIconBar().setEnabledModify(true);
                sGui.getMenu().getModify().setEnabled(true);
            }
            sGui.setSelectedTabContentLeft(0);
        } else {
            JOptionPane.showMessageDialog(null, "Impossible d'ouvrir cette pi\350ce dans le module Stock !");
        }
    }

    private Object getOpenPiece(int type, String pieceId) {
        switch (type) {
            case 0: // '\0'
                msgTab = "DEVIS client";
                pieceIcon = quoteSales;
                return crud.read(pieceId, 22);

            case 1: // '\001'
                msgTab = "COMMANDE client";
                pieceIcon = orderSales;
                return crud.read(pieceId, 22);

            case 2: // '\002'
                msgTab = "BL client";
                pieceIcon = deliverySales;
                return crud.read(pieceId, 22);

            case 3: // '\003'
                msgTab = "FACTURE client";
                pieceIcon = billSales;
                return crud.read(pieceId, 25);

            case 4: // '\004'
                msgTab = "AVOIR client";
                pieceIcon = assetSales;
                return crud.read(pieceId, 25);

            case 5: // '\005'
                msgTab = "COMMANDE fournisseur";
                pieceIcon = orderBuys;
                return crud.read(pieceId, 24);

            case 6: // '\006'
                msgTab = "FACTURE fournisseur";
                pieceIcon = billBuys;
                return crud.read(pieceId, 24);
        }
        return null;
    }

    private void actionPreparationUpdatePiece() {
        OrderStockController oCtrl = (OrderStockController) sGui.getSelectedPiece();
        if (oCtrl.getPieceId().equals(preparationCtrl.getPreparedPieceId()) && oCtrl.getPieceType() <= 2) {
            oCtrl.updateOrderRow(preparationCtrl.getListOrderRow());
        } else {
            JOptionPane.showMessageDialog(null, "La pi\350ce " + oCtrl.getPieceId() + " ne correspond pas \340 la pr\351paration !");
        }
    }

    private void actionReceptionPiece() {
        OrderStockController oCtrl = (OrderStockController) sGui.getSelectedPiece();
        if (oCtrl != null && oCtrl.getPieceType() == 5) {
            batchCtrl.setPieceId(oCtrl.getPieceId());
            batchCtrl.setSupplier(oCtrl.getSupplier());
            batchCtrl.setAgency(oCtrl.getAgency());
            org.jpgolay.me.order.OrderRow or = oCtrl.getSelectedOrderRow();
            if (or != null) {
                batchCtrl.setOrderRow(or);
            } else {
                JOptionPane.showMessageDialog(null, "S\351l\351ctionner une ligne !");
            }
        } else {
            JOptionPane.showMessageDialog(null, "Pi\350ce invalide !");
        }
    }

    private void actionReceptionUpdatePiece() {
        OrderStockController oCtrl = (OrderStockController) sGui.getSelectedPiece();
        if (oCtrl != null && oCtrl.getPieceType() == 5) {
            if (batchCtrl.getPieceId().equals(oCtrl.getPieceId())) {
                oCtrl.updateOrderRowReception(batchCtrl.getListOrderRow());
            } else {
                JOptionPane.showMessageDialog(null, "Pi\350ce invalide !");
            }
        } else {
            JOptionPane.showMessageDialog(null, "Pi\350ce invalide !");
        }
    }

    private void actionSendingPiece() {
        OrderStockController oCtrl = (OrderStockController) sGui.getSelectedPiece();
        if (oCtrl != null && oCtrl.getPieceType() == 2) {
            sendingCtrl.setSalesPiece(oCtrl.getSalesPiece());
            sendingCtrl.setCustomer(oCtrl.getCustomer());
            sendingCtrl.setAgency(oCtrl.getAgency());
        } else {
            JOptionPane.showMessageDialog(null, "Pi\350ce invalide !");
        }
    }

    private void actionSendingUpdatePiece() {
        OrderStockController oCtrl = (OrderStockController) sGui.getSelectedPiece();
        if (oCtrl != null && oCtrl.getPieceType() == 2) {
            if (sendingCtrl.getPieceId().equals(oCtrl.getPieceId())) {
                oCtrl.updateSalesPiece();
            } else {
                JOptionPane.showMessageDialog(null, "Pi\350ce invalide !");
            }
        } else {
            JOptionPane.showMessageDialog(null, "Pi\350ce invalide !");
        }
    }

    public void maximize() {
        try {
            sGui.setMaximum(true);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
    }

    public boolean init() {
        if (sGui == null || sGui.isClosed()) {
            return initAll();
        }
        if (sGui.isIcon()) {
            try {
                sGui.setIcon(false);
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
        }
        sGui.toFront();
        return false;
    }

    public StockGUI getGUI() {
        return sGui;
    }
    private StockGUI sGui;
    private Parameter param;
    private JDesktopPane desktop;
    private DeliveryController deliveryCtrl;
    private BatchController batchCtrl;
    private BatchListController batchListCtrl;
    private PreparationController preparationCtrl;
    private SendingController sendingCtrl;
    private SendingListController sendingListCtrl;
    private LogStockListController logStockListCtrl;
    private CustomerPanelController customerCtrl;
    private ItemPanelController itemCtrl;
    private SupplierPanelController supplierCtrl;
    private PieceListController pieceListCtrl;
    private StatisticsWPController statWPCtrl;
    private NewOrderController newOrderCtrl;
    private InventoryListController inventoryListCtrl;
    private ObjectCRUD crud;
    private String pieceId;
    private String msgTab;
    private Icon quoteSales;
    private Icon orderSales;
    private Icon deliverySales;
    private Icon billSales;
    private Icon assetSales;
    private Icon orderBuys;
    private Icon billBuys;
    private Icon pieceIcon;
    private Icon quoteSalesM;
    private Icon orderSalesM;
    private Icon deliverySalesM;
    private Icon billSalesM;
    private Icon assetSalesM;
    private Icon orderBuysM;
    private Icon billBuysM;
    private Icon pieceIconM;
    private int key;
    private PiecesPrint print;
    private boolean write;
}
